const tools = {};

// 后退
tools.back = (step) => {
	uni.navigateBack({
		delta: step,
		animationType: "pop-out",
		animationDuration: 200
	});
};

// 复制
tools.copy = (val) => {
	uni.setClipboardData({
		data: val,
		success() {
			uni.showToast({
				title: "复制成功",
				duration: 2500,
				icon: "none"
			})
		}
	})
}

//弹出消息
tools.toast = (val) => {
	uni.showToast({
		title: val,
		icon: "none",
		duration: 2500
	})
}

//页面跳转
tools.route = (path, mode="pop-in") => {
	uni.navigateTo({
		url: path,
		animationType: mode,
		animationDuration: 200
	})
}

tools.router = (path, value, value1) => {
	uni.navigateTo({
		url: path  + '?value=' + value + "&value1=" + value1,
	})
}

tools.navigate = (path) => {
	const token = uni.getStorageSync("ai-token") || ''
	if(token) {
		uni.navigateTo({
			url: path,
		})
		return false;
	}else{
		uni.navigateTo({
			url: "/pages/login/login"
		})
		return true;
	}
}

//保留小数点
tools.number = (value, type) => {
	if (value == null || value == '') {
		value = 0;
	}
	//处理科学计数法
	value = value.toFixed(8);
	var arr = (value + "").split(".");
	var a1 = arr[0];
	var a2 = 0;
	if (arr.length > 1) {
		a2 = arr[1];
		if (a2.length > type) {
			a2 = a2.substring(0, type);
		}
	}
	value = Number(a1 + "." + a2);
	return Number(value).toFixed(type)
}

tools.prePage = ()=>{
	let pages = getCurrentPages();
	let prePage = pages[pages.length - 2];
	// #ifdef H5
	return prePage;
	// #endif
	return prePage.$vm;
}

export default tools